/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package managedBeans;

import SessionBeans.UserSessionBean;
import javax.annotation.ManagedBean;
import javax.ejb.EJB;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;

/**
 *
 * @author Tomas
 */
@ManagedBean
@ViewScoped
public class CreateUserManagedBean {

    @EJB
    private UserSessionBean userSessionBean;
    protected String password1;
    protected String password2;
    protected String userName;
    protected String email;

    /**
     * Creates a new instance of CreateUserManagedBean
     */
    public CreateUserManagedBean() {
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public String getPassword1() {
        return password1;
    }

    public void setPassword1(String password1) {
        this.password1 = password1;
    }

    public String getPassword2() {
        return password2;
    }

    public void setPassword2(String password2) {
        this.password2 = password2;
    }

    public String getUserName() {
        return userName;
    }

    public void setUserName(String userName) {
        this.userName = userName;
    }

    public void createUser() {
        if (userSessionBean.checkUserName(userName)) {
            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR, "Error", "User name alredy been used!"));
            return ;
        }
        if (userSessionBean.checkEmail(email)) {
            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR, "Error", "Email adress alredy been used!"));
            return ;
        }
        
        userSessionBean.createUser(userName, password1, email);
        FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO, "User create", "Now you can login!"));
    }
}
